package com.ruoyi.controller;

import com.ruoyi.domain.UserHealthGoals;
import com.ruoyi.service.UserHealthGoalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/health/goals")
public class UserHealthGoalsController {

    @Autowired
    private UserHealthGoalsService userHealthGoalsService;

    /**
     * 获取用户最新的运动步数目标
     */
    @GetMapping("/latestStepGoal")
    public UserHealthGoals getLatestStepGoal(@RequestParam Long userId) {
        return userHealthGoalsService.getLatestStepGoal(userId);
    }

    /**
     * 获取用户最新的运动时间目标
     */
    @GetMapping("/latestTimeGoal")
    public UserHealthGoals getLatestTimeGoal(@RequestParam Long userId) {
        return userHealthGoalsService.getLatestTimeGoal(userId);
    }

    /**
     * 添加运动步数或运动时间目标
     */
    @PostMapping("/addGoal")
    public ResponseEntity<Map<String, Object>> addGoal(@RequestParam("userId") Long userId,
                                                       @RequestParam("name") Integer name,
                                                       @RequestParam("targetValue") Integer targetValue) {
        Map<String, Object> response = new HashMap<>();
        try {
            System.out.println("进入添加目标方法");
            System.out.println("接收到的目标数据：");
            System.out.println("userId: " + userId);
            System.out.println("name: " + name);
            System.out.println("targetValue: " + targetValue);

            // 调用 service 方法保存目标数据
            userHealthGoalsService.addGoal(userId, name, targetValue);

            // 返回成功的响应
            response.put("code", 200);
            response.put("message", "目标添加成功");
            return ResponseEntity.status(HttpStatus.OK).body(response);
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "目标添加失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

}
